import time
import pymysql
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

# 初始化无GUI谷歌
chromeOptions = Options()
chromeOptions.add_argument('--headless')
chromeOptions.add_argument('--disable-gpu')
browserPath = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
chromeOptions.binary_location = browserPath
browser = webdriver.Chrome(chrome_options=chromeOptions)

# 初始化MYSQL连接
connection = pymysql.Connect(
    host='127.0.0.1',
    port=3308,
    user='root',
    password='123456',
    # 指定字符集
    charset='utf8mb4',
    # 必须指定库名
    database='data-center'
)

# 请求页面
browser.get('https://macaostreets.iam.gov.mo/zh_cn/freguesiaindex.html')
pageHtml = browser.page_source
# print(pageHtml)

# 取堂区所有按钮
buttons = browser.find_elements(By.CSS_SELECTOR, '.FreguesiaButton')
# 逐一遍历
for btn in buttons:

    partName = btn.text
    # 写数据
    cursor = connection.cursor()
    cursor.execute('INSERT INTO `mazu-region` VALUES(%s, 0, %s, 1, %s, NOW())', [
        str(buttons.index(btn) + 101),
        partName,
        partName,
    ])
    connection.commit()

    btn.click()
    time.sleep(3)
    roads = browser.find_elements(By.CSS_SELECTOR, '.streetItems')
    for road in roads:
        roadName = road.text
        cursor = connection.cursor()
        cursor.execute('INSERT INTO `mazu-region` VALUES(%s, %s, %s, 2, %s, NOW())', [
            str((buttons.index(btn) + 101) * 100 + roads.index(road) + 1),
            str(buttons.index(btn) + 101),
            roadName,
            partName + ' -> ' + roadName,
        ])
        connection.commit()

